import {addEntity, cleanEntityById} from "../tools/common";
import {listDevice} from "/src/api/device/manage";
import {ScreenSpaceEventHandler, defined, ScreenSpaceEventType} from 'cesium'
import {addDateRange} from "/src/utils/DateUtile";

let deviceEntityIds = []
let deviceListData = []
let deviceListTotal = 0

let dateRange = []
let queryParams = {pageNum: 1, pageSize: 10000, categoryId: undefined}
let handler

export function getDeviceListByParam() {
    deviceEntityIds.forEach((deviceId) => {
        cleanEntityById(deviceId)
    })
    deviceEntityIds = []
    //获取设备列表
    listDevice(addDateRange(queryParams, dateRange)).then((res) => {
        deviceListData = res.rows
        deviceListTotal = res.total
        deviceListData.forEach((deviceInfo) => {
            deviceEntityIds.push(deviceInfo.deviceId)
            let drawPoint = {}
            drawPoint.x = deviceInfo.lng
            drawPoint.y = deviceInfo.lat
            drawPoint.z = 300
            if (deviceInfo.deviceIcon != null) {
                if (deviceInfo.status === "1") {
                    addEntity(drawPoint, deviceInfo.deviceCode, deviceInfo.deviceId, deviceInfo.deviceIcon + "-online")
                } else {
                    addEntity(drawPoint, deviceInfo.deviceCode, deviceInfo.deviceId, deviceInfo.deviceIcon + "-offline")
                }
            }
        })
    })
}

export function showDevice() {
    getDeviceListByParam()
    if (handler == null) {
        handler = new ScreenSpaceEventHandler(viewer.scene.canvas);
        handler.setInputAction(function (movement) {
            var pick = viewer.scene.pick(movement.position);
            if (defined(pick) && pick.id.id) {
                //设备ID
                var deviceId = pick.id.id
                //根据设备ID去查询显示详细信息
                console.log(deviceId)
            }
        }, ScreenSpaceEventType.LEFT_CLICK);
    }
}
